/**
 * Copyright (C), 2021, 杭州定川信息技术有限公司
 * FileName: MySQLProvider
 * Author:   guo@tt
 * Date:     2021/3/4 22:46
 * Description: MySQLProvider
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名         修改时间         版本号           描述
 */
package com.mt.roll.report.config;

/**
 * 〈MySQLProvider 〉
 *
 * @author guo@tt
 * @create 2021/3/4
 */

import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.ReportSheet;
import com.mt.roll.core.dal.service.ReportSheetServiceBiz;
import com.mt.roll.core.dto.LoginUser;
import com.mt.roll.report.service.ReportSheetService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Mysql // 该注解可以利用其 prefix属性值 + 类的属性名 在yml中配置属性值
 *
 * @author qiaolin
 * @version 2018年5月9日
 */

@Setter
@Component
public class DbProvider extends BaseService implements ReportProvider {
    private static final String NAME = "数据库服务器";

    /**
     * 特定前缀，ureport底层会调用 getPrefix 方法来获取报表操作的Provier类
     */
    private String prefix = "db:";

    private final static String FILE_SUFFIX = ".ureport.xml";

    private boolean disabled = false;

    private final ReportSheetService reportSheetService;

    private final ReportSheetServiceBiz reportSheetServiceBiz;

    @Autowired
    public DbProvider(ReportSheetService reportSheetService, ReportSheetServiceBiz reportSheetServiceBiz) {
        this.reportSheetService = reportSheetService;
        this.reportSheetServiceBiz = reportSheetServiceBiz;
    }

    @Override
    public InputStream loadReport(String file) {
        ReportSheet reportSheet = reportSheetService.getReportByName(getCorrectName(file));
        byte[] content = reportSheet.getContent();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
        return inputStream;
    }

    @Override
    public void deleteReport(String file) {
        reportSheetService.deleteReportByName(getCorrectName(file));
    }

    @Override
    public List<ReportFile> getReportFiles() {
        List<ReportSheet> list = reportSheetServiceBiz.list();
        List<ReportFile> reportList = new ArrayList<>();
        for (ReportSheet reportSheet : list) {
            Date updateTime = Date.from(reportSheet.getUpdateTime().atZone(ZoneId.systemDefault()).toInstant());
            reportList.add(new ReportFile(reportSheet.getName(), updateTime));
        }
        return reportList;
    }

    @Override
    public void saveReport(String file, String content) {
        LoginUser loginUser = getLoginUser();
        file = getCorrectName(file);
        ReportSheet reportSheet = reportSheetService.getReportByName(file);
        LocalDateTime currentDate = LocalDateTime.now();
        if (reportSheet == null) {
            reportSheet = new ReportSheet();
            reportSheet.setName(file);
            reportSheet.setFileName(file.replace(FILE_SUFFIX, ""));
            reportSheet.setContent(content.getBytes());
            reportSheet.setCreateTime(currentDate);
            reportSheet.setUpdateTime(currentDate);
            reportSheet.setCreatorId(loginUser == null ? null : loginUser.getUserId());
            reportSheetServiceBiz.save(reportSheet);
        } else {
            reportSheet.setContent(content.getBytes());
            reportSheet.setUpdateTime(currentDate);
            reportSheetServiceBiz.updateById(reportSheet);
        }
    }

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public boolean disabled() {
        return disabled;
    }

    @Override
    public String getPrefix() {
        return prefix;
    }

    /**
     * 获取没有前缀的文件名
     *
     * @param name
     * @return
     */
    private String getCorrectName(String name) {
        if (name.startsWith(prefix)) {
            name = name.substring(prefix.length());
        }
        return name;
    }
}
